package cn.ztuo.bitrade.controller.contract;

import cn.ztuo.bitrade.annotation.AccessLog;
import cn.ztuo.bitrade.constant.AdminModule;
import cn.ztuo.bitrade.constant.PageModel;
import cn.ztuo.bitrade.controller.common.BaseAdminController;
import cn.ztuo.bitrade.dto.ContractWalletDTO;
import cn.ztuo.bitrade.entity.QContractWallet;
import cn.ztuo.bitrade.entity.QMember;
import cn.ztuo.bitrade.model.screen.ContractWalletScreen;
import cn.ztuo.bitrade.service.ContractWalletService;
import cn.ztuo.bitrade.service.LocaleMessageSourceService;
import cn.ztuo.bitrade.service.MemberService;
import cn.ztuo.bitrade.util.MessageResult;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("contract/contract-wallet")
@RestController
@Slf4j
public class ContractWalletController extends BaseAdminController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private ContractWalletService contractWalletService;

    @Autowired
    private LocaleMessageSourceService messageSource;

    /**
     * 会员合约资产列表
     * @param pageModel
     * @param screen
     * @return
     */
    @RequiresPermissions("contract:contract-wallet:balance")
    @PostMapping("balance")
    @AccessLog(module = AdminModule.MEMBER, operation="合约余额管理")
    public MessageResult getBalance(PageModel pageModel, ContractWalletScreen screen){
        QContractWallet qContractWallet = QContractWallet.contractWallet;
        QMember qMember =QMember.member;
        List<Predicate> criteria = new ArrayList<>();
        if (StringUtils.hasText(screen.getAccount())) {
            criteria.add(qMember.username.like("%" + screen.getAccount() + "%")
                    .or(qMember.mobilePhone.like(screen.getAccount() + "%"))
                    .or(qMember.email.like(screen.getAccount() + "%"))
                    .or(qMember.realName.like("%" + screen.getAccount() + "%")));
        }

        if (!StringUtils.isEmpty(screen.getUnit())) {
            criteria.add(qContractWallet.coinId.eq(screen.getUnit()));
        }
        if (screen.getMaxAllBalance() != null) {
            criteria.add(qContractWallet.balance.add(qContractWallet.frozenBalance).loe(screen.getMaxAllBalance()));
        }
        if (screen.getMinAllBalance() != null) {
            criteria.add(qContractWallet.balance.add(qContractWallet.frozenBalance).goe(screen.getMinAllBalance()));
        }
        if (screen.getMaxBalance() != null) {
            criteria.add(qContractWallet.balance.loe(screen.getMaxBalance()));
        }
        if (screen.getMinBalance() != null) {
            criteria.add(qContractWallet.balance.goe(screen.getMinBalance()));
        }
        if (screen.getMaxFrozenBalance() != null) {
            criteria.add(qContractWallet.frozenBalance.loe(screen.getMaxFrozenBalance()));
        }
        if (screen.getMinFrozenBalance() != null) {
            criteria.add(qContractWallet.frozenBalance.goe(screen.getMinFrozenBalance()));
        }
        Page<ContractWalletDTO> page = contractWalletService.joinFind(criteria, qMember, qContractWallet, pageModel);
        return success(messageSource.getMessage("SUCCESS"), page);
    }
}
